package cn.doit.day05

import scala.collection.immutable
import scala.collection.mutable.ListBuffer
import scala.io.Source

/*
order.txt
order011,u001,300
order012,u002,200
order023,u006,100
order056,u007,300
order066,u003,500
order055,u004,300
order021,u005,300
order014,u001,100
order025,u005,300
order046,u007,30
order067,u003,340
order098,u008,310  ==> u008

user.txt
u001,hls,22,fengjie
u002,wangwu,31,lisi
u003,zhangyanru,22,tananpengyou
u004,laocao,26,fengyi
u005,mengqi,12,nvmengqi
u006,haolei,38,sb
u007,wanghongjing,24,wife
u009,wanghongjing,24,wife  ==> u009
 */
object _02_join案例 {
  def main(args: Array[String]): Unit = {
    //1.将数据分别读出来
    val order: List[String] = Source.fromFile("data/order.txt").getLines().toList
    val user: List[String] = Source.fromFile("data/user1.txt").getLines().toList
    //2.将数据按照,进行切割  order对象  user对象   返回元祖()  ()
    val order_tuple: List[(String, String, String)] = order.map(line => {
      val arr: Array[String] = line.split(",")
      (arr(0), arr(1), arr(2))
    })

    val user_map: Map[String, (String, String, String)] = user.map(line => {
      val arr: Array[String] = line.split(",")
      (arr(0), (arr(1), arr(2), arr(3)))
    }).toMap

    val res: ListBuffer[(String, String, String, String, String, String)] = new ListBuffer[(String, String, String, String, String, String)]()
    //left join
    for (elem <- order_tuple) {
      val user_id: String = elem._2
      val value: (String, String, String) = user_map.getOrElse(user_id, ("没得", "找不到", "真没有"))
      res.append((elem._1,elem._2,elem._3,value._1,value._2,value._3))
    }

    val order_user_ids: List[String] = order_tuple.map(_._2)
    val user_user_ids: List[String] = user_map.map(_._1).toList
    val diff_user: List[String] = user_user_ids.diff(order_user_ids)
    val diff_user1: List[String] = order_user_ids.diff(user_user_ids)

    for (elem <- diff_user) {
      val value: (String, String, String) = user_map.getOrElse(elem, (null, null, null))
      res.append((elem,null,null,value._1,value._2,value._3))
    }
    res.foreach(println)

    //单纯的循环  inner join
/*    for (elem <- order_tuple) {
      val user_id: String = elem._2
      val maybeTuple: Option[(String, String, String)] = user_map.get(user_id)
      if (!maybeTuple.isEmpty){
        val value: (String, String, String) = maybeTuple.get
        res.append((elem._1,elem._2,elem._3,value._1,value._2,value._3))
      }
    }*/



      //朕还想要一个full join


    //双重for循环，效率低
/*    val order_tuple: List[(String, String, String)] = order.map(line => {
      val arr: Array[String] = line.split(",")
      (arr(0), arr(1), arr(2))
    })

    val user_tuple: List[(String, String, String,String)] = user.map(line => {
      val arr: Array[String] = line.split(",")
      (arr(0), arr(1), arr(2),arr(3))
    })

    //用可变的集合
    val res: ListBuffer[(String, String, String, String, String, String)] = new ListBuffer[(String, String, String, String, String, String)]()

    //3.双重for循环 进行inner join
    for (o <- order_tuple) {
      for (u <- user_tuple) {
        if(o._2.equals(u._1)){
          //添加元素
          res.append((o._1,o._2,o._3,u._2,u._3,u._4))
        }else{
        }


      }
    }

    res.foreach(println)*/


  }

}
